<?php
$code = <<<EOF
package http

import (
    "git.inke.cn/bpc/framework/server/utils/collections"
    "{$data['root_path_name']}/{$data["service_name"]}/api/code"
    "{$data['root_path_name']}/{$data["service_name"]}/model"
	"encoding/json"
	"git.inke.cn/BackendPlatform/golang/logging"
	httpserver "git.inke.cn/inkelogic/daenerys/http/server"
	"io/ioutil"
)

/**
 * @Description:新增
 * @param c
 */
func {$data['model_name']}Add(c *httpserver.Context) {
    logger := logging.For(c.Ctx, "func", "{$data['model_name']}Add")
	var req model.{$data['model_name']}AddReq
	if err := c.Bind(c.Request, &req); err != nil {
		logger.Errorf("bind req failed, err:%s", err)
		c.JSONAbort(nil, code.InvalidParam)
		return
	}

	record ,err := svc.{$data['model_name']}Add(c.Ctx, req)
	if err != nil {
		logger.Errorf("[{$data['model_name']}Add] error,req:%v, err:%+v", req, err)
		c.JSONAbort(nil, err)
		return
	}

	c.JSON(record, code.Success)
}

/**
 * @Description:删除
 * @param c
 */
func {$data['model_name']}Delete(c *httpserver.Context) {
    logger := logging.For(c.Ctx, "func", "{$data['model_name']}Delete")
	id := c.QueryInt64("id")
	if id <= 0 {
		c.JSONAbort(nil, code.InvalidParam)
	}

	err := svc.{$data['model_name']}Delete(c.Ctx, id)
	if err != nil {
		logger.Errorf("[{$data['model_name']}Delete] error,id:%v, err:%+v", id, err)
		c.JSONAbort(nil, err)
		return
	}

	c.JSON(nil, code.Success)
}

/**
 * @Description: 更新（此接口需要自定义可更改的字段值）
 * @param c
 */
func {$data['model_name']}Update(c *httpserver.Context) {
	logger := logging.For(c.Ctx, "func", "{$data['model_name']}Update")
	req, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		logger.Errorf("[ReadAll] error,req:%v, err:%+v", req, err)
		return
	}
	reqData := map[string]interface{}{}
	err = json.Unmarshal(req, &reqData)
	if err != nil {
		logger.Errorf("[Unmarshal] error, err:%+v", err)
		return
	}

	//校验参数是否和定义的一致
	validParams := make([]string, 0)
	validParams = []string{"id"}//todo 【定义能修改的字段】：如果注释此处则不校验
	if len(validParams) > 0 {//此逻辑表示需要校验参数的字段
		//数量上不相等，直接报错
		if len(validParams) != len(reqData) {
			logger.Errorf("[params] error, err:%+v", err)
			c.JSONAbort(nil, code.InvalidParam)
			return
		}

		for i,_ := range reqData{
			//客户端传过来的参数和服务端定义的参数不一致
			if !collections.Contain(validParams, i) {
				logger.Errorf("[params] error, err:%+v", err)
				c.JSONAbort(nil, code.InvalidParam)
				return
			}
		}
	}

	err = svc.{$data['model_name']}Update(c.Ctx, reqData)
	if err != nil {
		c.JSONAbort(nil, err)
		return
	}

	c.JSON(nil, code.Success)
}

/**
 * @Description: 详情,参数为id
 * @param c
 */
func {$data['model_name']}Detail(c *httpserver.Context) {
	logger := logging.For(c.Ctx, "func", "{$data['model_name']}Detail")
	var req model.{$data['model_name']}DetailReq
	if err := c.Bind(c.Request, &req); err != nil {
		logger.Errorf("{$data['model_name']}Detail  param bind fail , err : %s", err)
		c.JSONAbort(nil, code.InvalidParam)
		return
	}
	
	if req.Id <= 0 {
		c.JSONAbort(nil, code.InvalidParam)
	}

	resp, err := svc.{$data['model_name']}Detail(c.Ctx, req)
	if err != nil {
		logger.Errorf("{$data['model_name']}Detail error , id: %+v error: %s", req.Id, err)
		c.JSONAbort(nil, err)
		return
	}

	c.JSON(resp, code.Success)
}


/**
 * @Description: 列表，支持分页
 * @param c
 */
func {$data['model_name']}List(c *httpserver.Context) {
    logger := logging.For(c.Ctx, "func", "{$data['model_name']}List")
	var req model.{$data['model_name']}ListReq

	if err := c.Bind(c.Request, &req); err != nil {
		logger.Errorf("{$data['model_name']}List  param bind fail , err : %s", err)
		c.JSONAbort(nil, code.InvalidParam)
		return
	}
	
	if req.Page < 0 || req.Size <= 0 {
		logger.Errorf("page or size param err,req:%v",req)
		c.JSONAbort(nil, code.InvalidParam)
		return
	}

	res := svc.{$data['model_name']}List(c.Ctx, req)

	c.JSON(res, code.Success)
}
EOF;
?>
<?= $code?>
